﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Linq;

namespace Chambills.PopUpBehavior
{
    public class DisableBackGroundBehaviour : Behavior<Grid>
    {
         public static readonly DependencyProperty BackgroundProperty =
            DependencyProperty.Register("Background", typeof(Brush), typeof(DisableBackGroundBehaviour), new PropertyMetadata(Brushes.Transparent));

         public static readonly DependencyProperty OpacityProperty =
            DependencyProperty.Register("Opacity", typeof(double), typeof(DisableBackGroundBehaviour), new PropertyMetadata(.4));

         public static readonly DependencyProperty EnabledProperty =
            DependencyProperty.Register("Enabled", typeof(bool), typeof(DisableBackGroundBehaviour), new PropertyMetadata(true, OnEnabledChanged));

        private readonly Rectangle skin;

        public DisableBackGroundBehaviour()
        {
             skin = new Rectangle { Name = "DisableBackGroundBehaviour_Skin"};
        }


        private static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var behaviour = d as DisableBackGroundBehaviour;
            if (behaviour == null || behaviour.AssociatedObject == null)
                return;

            if(Convert.ToBoolean(e.NewValue))
                behaviour.OnAttached();
            else
                behaviour.OnDetaching();
        }



        protected override void OnAttached()
        {
            base.OnAttached();
            if (!Enabled)
                return;

           
            skin.SetValue(Panel.ZIndexProperty, 999);
            skin.SetValue(Grid.RowSpanProperty, Math.Max(1, AssociatedObject.RowDefinitions.Count));
            skin.SetValue(Grid.ColumnSpanProperty, Math.Max(1, AssociatedObject.ColumnDefinitions.Count));
            skin.Cursor = Cursors.Wait;
            skin.ForceCursor = true;

            skin.SetBinding(Shape.FillProperty, new Binding("Background") {Source = this, Mode = BindingMode.TwoWay});
            skin.SetBinding(UIElement.OpacityProperty, new Binding("Opacity") {Source = this, Mode = BindingMode.TwoWay});



            AssociatedObject.Children.Add(skin);

            PreventAccessToDisabledControls();

        }

        private void PreventAccessToDisabledControls()
        {
            KeyboardNavigation.SetTabNavigation(AssociatedObject, KeyboardNavigationMode.Cycle);
            KeyboardNavigation.SetDirectionalNavigation(AssociatedObject, KeyboardNavigationMode.Cycle);
            KeyboardNavigation.SetControlTabNavigation(AssociatedObject, KeyboardNavigationMode.Cycle);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
           AssociatedObject.Children.Remove(skin);
        }

        public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }

        public double Opacity
        {
            get { return (double)GetValue(OpacityProperty); }
            set { SetValue(OpacityProperty, value); }
        }

        public bool Enabled
        {
            get { return (bool)GetValue(EnabledProperty); }
            set { SetValue(EnabledProperty, value); }
        }

    }
}